package br.inf.mfpo.fra4and.exemplo.adapters;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import br.inf.mfpo.fra4and.R;
import br.inf.mfpo.fra4and.exemplo.columns.PessoasColumns;

public class PessoasCursorAdapter extends SimpleCursorAdapter {

	public PessoasCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
		super(context, layout, c, from, to);

		final Context ctx = context;

		setViewBinder(new ViewBinder() {
			@Override
			public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
				if (cursor == null || cursor.getCount() == 0) {
					return false;
				}

				final String colName = cursor.getColumnName(columnIndex);

				if (colName.equals(PessoasColumns.SEXO) && view != null) {
					String[] sexes = ctx.getResources().getStringArray(R.array.genero);
					int sexo = cursor.getInt(columnIndex);
					String text = sexes[sexo];
					if (view instanceof TextView) {
						setViewText((TextView) view, text);
					} else if (view instanceof ImageView) {
						setViewImage((ImageView) view, text);
					} else {
						throw new IllegalStateException(view.getClass().getName() + " is not a " + " view that can be bounds by this SimpleCursorAdapter");
					}
					return true;
				}

				return false;
			}
		});
	}

}
